package cn.dnui_xcz826.dnuifood_16110100826.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ShopBean;
import cn.dnui_xcz826.dnuifood_16110100826.controller.Constants;
import cn.dnui_xcz826.dnuifood_16110100826.controller.FoodListActivity;

/**
 * Created by Administrator on 2018/11/1.
 */

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ShopBean> list;
    private LayoutInflater layoutInflater;

    public ShopAdapter(Context context, List list) {
        this.context=context;
        this.list=list;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<ShopBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (list!=null)
            return list.size();
        else
            return 0;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ShopBean entity = list.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());
        Picasso.with(context).load(Constants.BASE_URL+entity.getPic()).into(viewHolder.image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FoodListActivity.class);
                intent.putExtra("shop_id",(list.get(position)).getShop_id()+"");
                intent.putExtra("shop_name",(list.get(position)).getShopname());
                intent.putExtra("phonenum", entity.getPhonenum());
                context.startActivity(intent);
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RatingBar res_bar;
        TextView res_name, res_address;
        ImageView image;
        public ViewHolder(View view) {
            super(view);
            res_name = (TextView) view.findViewById(R.id.tvTitle);
            res_bar = (RatingBar) view.findViewById(R.id.ratingBar1);
            image = (ImageView) view.findViewById(R.id.imageView);
            res_address = (TextView) view.findViewById(R.id.description);
        }
    }
}